package common;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

@SuppressWarnings("unchecked")
public class ClickWritable implements WritableComparable{
	private long pv = 0;
	private long click = 0;
	private long bargain = 0;
	
	public ClickWritable(){		
	}
	
	public void setValue(MetricsRecord mr){
		pv = mr.getPVCount();
		click = mr.getClickCount();
		bargain = mr.getBargainCount();
	}
	
	public long getPVCount(){
		return pv;
	}
	
	public long getClickCount(){
		return click;
	}
	
	public long getBargainCount(){
		return bargain;
	}
	
	@Override
	public void write(DataOutput out) throws IOException{
		out.writeLong(pv);
		out.writeLong(click);
		out.writeLong(bargain);
	}
	

	@Override
	public int compareTo(Object o){
		ClickWritable cw = (ClickWritable)o;
		long thisClick = click;
		long thatClick = cw.click;
		return (thisClick < thatClick ? -1 : (thisClick == thatClick ? 0 : 1));
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		pv = in.readLong();
		click = in.readLong();
		bargain = in.readLong();
		
	}
	
	@Override
	public String toString(){
		
		return pv + "\t" + click + "\t" + bargain;  
	}
	
	public void addClick(ClickWritable cw){
		pv += cw.pv;
		click += cw.click;
		bargain += cw.bargain;
	}

}
